/** 
* @file vmu_pde.cpp
* Page Directory Entries (PDE) aid routines implementation.
* Provides abstraction interface to aid in managment of PDEs.
* Page Directory Entry is what represent PAGE TABLE.
* Page Table is array of Pages (PTEs). It is 32 bit structure
* where frame parameter pointing to address whete Page Table array begins.
*
* Copyrights 2010 Michal Saman, m.saman@designplus.cz.
* This source code is release under the Apache License 2.0.
* http://www.apache.org/licenses/LICENSE-2.0
*/

#include <stdint.h>
#include <mem/vmu_pde.h>
#include <mem/pmu.h>
#include <main/kprintf.h>

/**
* Sets a flag in the page directory entry (PAGE TABLE).
* @param e Pointer to PDE.
* @param attrib Attribe to set.
*/
void pde_add_attrib(pde *e, uint32_t attrib) {
	*e |= attrib;
}

/**
* Clears a flag in the page directory entry.
* @param e Pointer to PDE.
* @param attrib Attribe to clear.
*/
void pde_del_attrib(pde *e, uint32_t attrib) {
	*e &= ~attrib;
}

/**
* Sets a frame address to page directory entry. (addr of page table).
* @param e Pointer to PDE.
* @param addr Physical address of page table.
*/
void pde_set_frame(pde *e, physical_addr addr) {
	//kprintf("PDE: 0x%x, ~Mask: 0x%x, ~Mask AND PDE: 0x%x, frame addr: 0x%x\n", *e, ~PDE_FRAME, (*e & ~PDE_FRAME),(addr / PMU_BLOCK_SIZE));
	*e = (*e & ~PDE_FRAME) | ((addr / PMU_BLOCK_SIZE) << 12);
}

/**
* Get page directory frame addres - base addres of PAGE TABLE.
* @param e PDE
*/
physical_addr pde_get_frame(pde e) {
	return (e >> 12) * PMU_BLOCK_SIZE;
}

/**
* Test if page table is present.
* @param e PDE
*/
bool pde_is_present(pde e) {
	return e & PDE_PRESENT;
}

/**
* Test if page table is user mode.
* @param e PDE
*/
bool pde_is_user(pde e) {
	return e & PDE_USER;
}

/**
* Test if page table contains 4mb pages.
* @param e PDE
*/
bool pde_is_4mb(pde e) {
	return e & PDE_4MB;
}

/**
* Test if page table is writable.
* @param e PDE
*/
bool pde_is_writable(pde e) {
	return e & PDE_WRITABLE;
}
